package com.cash.app.custom

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.cash.app.R
import com.cash.app.databinding.AppActionBarBinding

@SuppressLint("CustomViewStyleable")
class AppActionBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private val binding: AppActionBarBinding = AppActionBarBinding.inflate(
        LayoutInflater.from(context), this, true
    )

    init {
        context.obtainStyledAttributes(attrs, R.styleable.AppActionBar).apply {

            setBarColor(getColor(R.styleable.AppActionBar_barColor, Color.TRANSPARENT))

            // center title
            if (getBoolean(R.styleable.AppActionBar_showTitle, true)) {
                setTitle(getString(R.styleable.AppActionBar_title) ?: "")
                setTitleSize(
                    getDimension(
                        R.styleable.AppActionBar_titleSize, context.resources.getDimension(
                            R.dimen.app_action_bar_text_size
                        )
                    )
                )
                setTitleColor(
                    getColor(
                        R.styleable.AppActionBar_titleColor, resources.getColor(R.color.black_2)
                    )
                )
                showTitle(true)
            }

            // left back p_icon
            if (getBoolean(R.styleable.AppActionBar_showBackIcon, true)) {
                setBackIcon(
                    getResourceId(
                        R.styleable.AppActionBar_backIcon, R.mipmap.back3
                    )
                )
                setBackSize(
                    getDimension(
                        R.styleable.AppActionBar_backIconSize, context.resources.getDimension(
                            R.dimen.app_action_bar_icon_size
                        )
                    )
                )
                showBack(true)
            }

            // left text
            if (getBoolean(R.styleable.AppActionBar_showLeftText, false)) {
                setLeftText(getString(R.styleable.AppActionBar_leftText) ?: "")
                setLeftTextSize(
                    getDimension(
                        R.styleable.AppActionBar_leftTextSize, context.resources.getDimension(
                            R.dimen.app_action_bar_text_size
                        )
                    )
                )
                setLeftTextColor(
                    getColor(
                        R.styleable.AppActionBar_leftTextColor, resources.getColor(R.color.black_2)
                    )
                )
                showLeftText(true)
            }

            // right p_icon
            if (getBoolean(R.styleable.AppActionBar_showRightIcon, false)) {
                setRightIcon(
                    getResourceId(
                        R.styleable.AppActionBar_rightIcon, R.mipmap.app_icon
                    )
                )
                setRightIconSize(
                    getDimension(
                        R.styleable.AppActionBar_rightIcon_size, context.resources.getDimension(
                            R.dimen.app_action_bar_icon_size
                        )
                    )
                )
                showRightIcon(true)
            }

            // right text
            if (getBoolean(R.styleable.AppActionBar_showRightText, false)) {
                setRightText(getString(R.styleable.AppActionBar_rightText) ?: "")
                setRightTextSize(
                    getDimension(
                        R.styleable.AppActionBar_rightTextSize, context.resources.getDimension(
                            R.dimen.app_action_bar_text_size
                        )
                    )
                )
                setRightTextColor(
                    getColor(
                        R.styleable.AppActionBar_rightTextColor, resources.getColor(R.color.black_2)
                    )
                )
                showRightText(true)
            }

            recycle()
        }
    }

    fun setBarColor(barColorResId: Int) {
        binding.actionBarContainer.setBackgroundColor(barColorResId)
    }

    fun setTitle(text: String) {
        binding.actionBarTitle.text = text
    }

    fun setTitleSize(textSize: Float) {
        binding.actionBarTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
    }

    fun setTitleColor(textColorResId: Int) {
        binding.actionBarTitle.setTextColor(textColorResId)
    }

    fun showTitle(show: Boolean) {
        binding.actionBarTitle.isVisible = show
    }

    fun setBackIcon(iconResId: Int) {
        binding.actionBarBack.setImageResource(iconResId)
    }

    fun setBackSize(iconSize: Float) {
        binding.actionBarBack.layoutParams.width = iconSize.toInt()
        binding.actionBarBack.layoutParams.height = iconSize.toInt()
    }

    fun showBack(show: Boolean) {
        binding.actionBarBack.isVisible = show
    }

    fun setBackClickListener(listener: OnClickListener) {
        binding.actionBarBack.setOnClickListener(listener)
    }

    fun setLeftText(text: String) {
        binding.actionBarLeftTv.text = text
    }

    fun setLeftTextSize(textSize: Float) {
        binding.actionBarLeftTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
    }

    fun setLeftTextColor(textColorResId: Int) {
        binding.actionBarLeftTv.setTextColor(textColorResId)
    }

    fun setLeftTextClickListener(listener: OnClickListener) {
        binding.actionBarLeftTv.setOnClickListener(listener)
    }

    fun showLeftText(show: Boolean) {
        binding.actionBarLeftTv.isVisible = show
    }

    fun setRightIcon(iconResId: Int) {
        binding.actionBarRightIcon.setImageResource(iconResId)
    }

    fun setRightIconSize(iconSize: Float) {
        binding.actionBarRightIcon.layoutParams.width = iconSize.toInt()
        binding.actionBarRightIcon.layoutParams.height = iconSize.toInt()
    }

    fun showRightIcon(show: Boolean) {
        binding.actionBarRightIcon.isVisible = show
    }

    fun setRightIconClickListener(listener: OnClickListener) {
        binding.actionBarRightIcon.setOnClickListener(listener)
    }

    fun setRightText(text: String) {
        binding.actionBarRightTv.text = text
    }

    fun setRightTextSize(textSize: Float) {
        binding.actionBarRightTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
    }

    fun setRightTextColor(textColorResId: Int) {
        binding.actionBarRightTv.setTextColor(textColorResId)
    }

    fun setRightTextClickListener(listener: OnClickListener) {
        binding.actionBarRightTv.setOnClickListener(listener)
    }

    fun showRightText(show: Boolean) {
        binding.actionBarRightTv.isVisible = show
    }
}